package gold.digger;

import gold.utils.InputUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1356 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int[] sortByBits(int[] arr) {
            List<Integer> intList = new ArrayList<Integer>();
            for (int num : arr) {
                intList.add(num);
            }

            intList.sort(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    int c1 = getOneCount(o1), c2 = getOneCount(o2);
                    return c1 == c2 ? o1 - o2 : c1 - c2;
                }
            });

            int i = 0;
            for (Integer num : intList) {
                arr[i++] = num;
            }

            return arr;
        }

        public int getOneCount(int num) {
            int count = 0;
            while (num > 0) {
                num &= (num - 1);
                ++count;
            }

            return count;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1024,512,256,128,64,32,16,8,4,2,1]");
        System.out.println(Arrays.toString(solution.sortByBits(arr)));
//        System.out.println(solution.getOneCount(7));
    }

    public static void main(String[] args) throws Exception {
        LC1356 an = new LC1356();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
